/*
 * Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
 */
package com.pitop.material.service;

import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.MaterialException;
import com.pitop.core.tools.StringTools;
import com.pitop.material.VO.MaterialVO;
import com.pitop.material.api.MaterialFeignApi;
import com.pitop.material.api.MaterialSupplierFeignApi;
import com.pitop.material.entity.Material;
import com.pitop.material.entity.MaterialState;
import com.pitop.material.entity.MaterialSupplier;
import com.pitop.mould.api.MouldMaterialFeignApi;
import com.pitop.mould.entity.MouldMaterial;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 供应料材
 *
 * @author berton
 */
@Service("materialSVImpl")
@Slf4j
public class MaterialSVImpl {

    @Autowired
    private MaterialFeignApi materialFeignApi;

    @Autowired
    private MaterialSupplierFeignApi materialSupplierFeignApi;

    @Autowired
    private MouldMaterialFeignApi mouldMaterialFeignApi;

    /**
     * 加载一个对象Material
     *
     * @param id   id
     * @param code 编码
     * @return Material
     */
    public MaterialVO load(Long id, String code) {
        if (id == null && code == null) {
            throw new MaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return materialFeignApi.load(id, code);
    }

    /**
     * 加载一个对象Material 通过id
     *
     * @param id id
     * @return Material
     */
    public MaterialVO loadById(Long id) {
        if (id == null) {
            throw new MaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return materialFeignApi.loadById(id);
    }

    /**
     * 加载一个对象Material 通过code
     *
     * @param code 编码
     * @return Material
     */
    public MaterialVO loadByCode(String code) {
        if (code == null) {
            throw new MaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return materialFeignApi.loadByCode(code);
    }

    /**
     * 加载一个对象Material 通过suppierCode
     *
     * @param suppierCode 供应商编码
     * @return Material
     */
    public Material loadBySuppierCode(String suppierCode) {
        if (suppierCode == null) {
            throw new MaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return materialFeignApi.loadBySuppierCode(suppierCode);
    }


    /**
     * 删除对象Material
     *
     * @param id          *
     * @param code        编码*
     * @param suppierCode 供应商编码
     */

    public void delete(Long id, String code, String suppierCode) {
        if (id == null && code == null && suppierCode == null) {
            throw new MaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        materialFeignApi.delete(id, code, suppierCode);
    }


    /**
     * 查询Material分页
     *
     * @param material 对象
     * @param curPage  当前页
     * @param pageSize 分页条数
     * @return List<Material>
     */
    public List<MaterialVO> list(Material material, int curPage, int pageSize) {
        return materialFeignApi.list(material, curPage, pageSize);
    }

    /**
     * 查询Material分页
     *  @param assemblyCode 对象
     * @param state
     * @param name
     * @param curPage  当前页
     * @param pageSize 分页条数   @return List<Material>
     * */
    public List<MaterialVO> listForAssembly(String assemblyCode, String state, String name, int curPage, int pageSize) {
        return materialFeignApi.listForAssembly(assemblyCode, state, name, curPage, pageSize);
    }

 /**
     * 查询Material分页
  * @param bomCode 对象
  * @param state
  * @param name
  * @param curPage  当前页
  * @param pageSize 分页条数   @return List<Material>    */
    public List<MaterialVO> listForBom(String bomCode, String state, String name, int curPage, int pageSize) {
        return materialFeignApi.listForBom(bomCode, state, name , curPage, pageSize);
    }

    /**
     * 查询Material分页
     *
     * @param id       * @param code  编码* @param suppierCode  供应商编码
     * @param curPage  当前页
     * @param pageSize 分页条数
     * @return List<Material>
     */
    public List<MaterialVO> list(Long id, String code, int curPage, int pageSize) {
        if (curPage < 0) {
            curPage = 1;
        }

        if (pageSize < 0) {
            pageSize = Page.limit;
        }

        Material material = new Material();
        material.setId(id);
        material.setCode(code);

        return materialFeignApi.list(material, curPage, pageSize);
    }


    public int count(Long id, String code) {
        return materialFeignApi.count(id, code);
    }


    public int count(Material material) {
        return materialFeignApi.count(material);
    }


    public int countForAssembly(String assemblyCode, String state, String name) {
        return materialFeignApi.countForAssembly(assemblyCode, state, name);
    }


    public int countForBom(String bomCode, String state, String name) {
        return materialFeignApi.countForBom(bomCode, state, name);
    }

    public int countByCategoryName(Material material) {
        return materialFeignApi.countByCategoryName(material);
    }

    /**
     * 保存
     * 1.数据合法性验证
     * ****数据非空验证
     * ****供应材料名称是否已经存在
     * 2.保存材料
     *
     * @param materialVO 实体
     * @param account
     * @param accountCode
     */
    public Material save(MaterialVO materialVO, String account, String accountCode) {
        //1.数据合法性验证
        if (materialVO == null) {
            throw new MaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        //供应材料名称是否已经存在
        Material material1Flag = materialFeignApi.loadByName(materialVO.getName());
        if (material1Flag != null) {
            throw new MaterialException(BaseException.BaseExceptionEnum.Exists);
        }
        //2.保存材料信息
        Material material = StringTools.convertObject(materialVO, Material.class);
        materialFeignApi.build(material, account, accountCode);
        material = materialFeignApi.loadByName(materialVO.getName());
        //保存供应商
        if (material != null) {
            List<MaterialSupplier> materialSuppliers = materialVO.getMaterialSuppliers();
            for (MaterialSupplier materialSupplier : materialSuppliers) {
                materialSupplier.setMaterialCode(material.getCode());
                materialSupplierFeignApi.build(materialSupplier);
            }
        }

        //保存模具信息
        if(material != null && StringTools.isNotEmpty(materialVO.getMouldCode())) {
            MouldMaterial mouldMaterial= new MouldMaterial();
            mouldMaterial.setMaterialCode(material.getCode());
            mouldMaterial.setMouldCode(materialVO.getMouldCode());
            mouldMaterialFeignApi.build(mouldMaterial);
        }
        return material;
    }

    /**
     * 更新
     * 1.数据合法性验证
     * ****数据非空验证
     * ****供应材料名称是否已经存在
     * 2.更新材料
     *
     * @param materialVO 实体
     */
    public void modify(MaterialVO materialVO) {
        if (materialVO == null) {
            throw new MaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        //供应材料名称是否已经存在
        Material material1Flag = materialFeignApi.loadByName(materialVO.getName());
        if (material1Flag != null && !materialVO.getCode().equals(material1Flag.getCode())) {
            throw new MaterialException(BaseException.BaseExceptionEnum.Exists);
        }
        //2.更新材料信息
        Material material = StringTools.convertObject(materialVO, Material.class);
        materialFeignApi.modify(material);
        material = materialFeignApi.loadByName(materialVO.getName());
        //保存材料代理商关系
        materialSupplierFeignApi.delete(null, null, material.getCode());
        List<MaterialSupplier> materialSuppliers = materialVO.getMaterialSuppliers();
        for (MaterialSupplier materialSupplier : materialSuppliers) {
            materialSupplier.setMaterialCode(material.getCode());
            materialSupplierFeignApi.build(materialSupplier);
        }
        //保存模具信息
        mouldMaterialFeignApi.delete(null, material.getCode(), materialVO.getMouldCode());
        if(material != null && StringTools.isNotEmpty(materialVO.getMouldCode())) {
            MouldMaterial mouldMaterial= new MouldMaterial();
            mouldMaterial.setMaterialCode(material.getCode());
            mouldMaterial.setMouldCode(materialVO.getMouldCode());
            mouldMaterialFeignApi.build(mouldMaterial);
        }
    }


    /**
     * 更新状态
     * ****数据非空验证
     * ****供应材料名称是否已经存在
     * 2.更新材料
     *
     * @param code  材料编码
     * @param state 材料状态
     */
    public void updateSate(String code, MaterialState state) {
        //1.数据合法性验证
        if (StringTools.isEmpty(code)) {
            throw new MaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (state == null) {
            throw new MaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        //供应材料名称是否已经存在
        MaterialVO material1Flag = materialFeignApi.loadByCode(code);
        if (material1Flag == null) {
            throw new MaterialException(BaseException.BaseExceptionEnum.Result_Not_Exist);
        }
        //2.更新材料状态
        materialFeignApi.updateState(code, state);
    }

}
